/**
 * Created by Administrator on 2016/8/11.
 */
define(["require",'config/appregister',"business/commpanyVideo/service/http","business/commpanyVideo/directive/upload","business/Companyinfor/service/chinese"],function(require, app, httpServices){
    app.controller('commpanyVideoCtrl',function($scope,$timeout,$state){
        $scope.isDetailPage = true;
        $scope.isMainpage = false;
        $scope.add = {
            title:"",
            labelIds:"你好",
            tcId:"",
            scId:"",
            thirdId:"",
            type:"",
            introduction:"",
            price:"",
            userId:"",
            copyright:"",
            visibility:"",
            previewPath:"",
            agreeFlag:0,
            videoFlag:'',
            jsonStr:[]
        }
        $scope.noContent2="无更多选项"
		$scope.goguiz=function(){
			 $state.go("footerCtrl.helpDetailCtrl",{helpId:4,helpDetailId:1})
		}
        $scope.tags2 = [//ngInputTab自己写方法

        ];
        $scope.saveTagsname=function(a){
            if(a!=undefined){
                if($scope.tags2.length == 3){
                    alert('最多只能添加三个标签哦，亲');
                    return;
                }
                if($scope.tags2.indexOf(a) != -1){
                    alert("标签不能相同哦")
                }else{
                    $scope.tags2.push(a);
                }
            }

        }
        $scope.deleteInput=function($index){
            $scope.tags2.splice($index,1);
        }



        $scope.isShowSend = 'send';
        $scope.sendShow = function(item){
            $scope.isShowSend = item;
            if(item == 'vedio'){
                $scope.add.videoFlag = 1;
            }else if(item == 'course'){
                $scope.add.videoFlag = 0;
            }
        }
        $scope.teacheron="请选择"
        $scope.showTwoDatas2=function(item){
            $scope.teacheron=item.name;
            $scope.add.userId = item.followUserId;

        }
        $scope.saveTeacher=function(){
        	 $scope.teacheron="请选择"
        }
        $scope.goTo = function(state){
            $state.go(state,{},{reload:true})
        }

        $scope.stus=[{"id":"1","name":"张三"},{"id":"2","name":"李四"}];
        $scope.change=function(item){
            console.log(item)
        }
        /*限制textatea的字数*/
        $scope.checkText = function () {
            $scope.length=$scope.add.introduction.length;
                $scope.add.introduction = $scope.add.introduction.substr(0,2000);
        
        };
        /*进度条*/
        function progress(){
            var vm = $scope.vm = {};
            vm.value = 50;
            vm.style = 'progress-bar-danger';
            vm.showLabel = true;
            vm.striped = true;
            $scope.selectValue = function (){
               /* console.log(vm.style);*/
            };
            var index = 0;
            var timeId = 500;
            $scope.count = function(){
                var start = $interval(function(){
                    vm.value =  ++index;
                    if (index > 99) {
                        $interval.cancel(start);
                    }
                    if (index == 60) {
                        index = 99;
                    }
                }, timeId);
            };
        }
        progress();
        /*判断标题名是否为空*/
        $scope.Red=2;
        $scope.add.title=function(name){
            $scope.unable="标题名不能为空"
            var value= /[@#\$%\^&\*]+/g;
            if( value.test(name)){
                $scope.unable="禁止输入非法字符";
                $scope.nameShow=1;
                $scope.Red=1;
            } else if(name){
                $scope.nameShow=2;
                $scope.Red=2;
            } else{
                $scope.nameShow=1;
                $scope.Red=1;
            }
        }
        /*动态添加删除文件夹*/
        $scope.creatCons=[];
            $scope.add=function(a){
                var obj = {
                    id:0,
                    name:a
                }///obj发送给后台即可文件夹分类
                $scope.creatCons.push(obj);
            }
            $scope.del=function(item,idx){
                $scope.creatCons.splice(idx,1);
                var req ={
                    type:"DELETE",
                    uri:"deleteCourseType",
                    data:{
                        typeId:item.id
                    }
                };
                httpServices.postcoursetype(req).success(function(response){
                    $scope.$apply(function(){
                      /* console.log(response.code)*/
                    })
                });
            }
            $scope.addcLass = function(item,index){
                item.isHover = !item.isHover;
            }
            $scope.hideNow=function(){
                $scope.isShowSend="send"
                $scope.chapterCon=[];
            }
            /*获取课程分类*/
           $scope.showpropmt=function(){
                $scope.showpropmts=1;
                var req ={
                    type:"GET",
                    uri:"getCourseType",
                    data:{}
                };
                httpServices.postcoursetype(req).success(function(response){
                    $scope.$apply(function(){
                        $scope.creatCons=response.data;
                    })
                });
            }
            //发送课程分类数据到后台
            $scope.saveCourseType=function(){
                var str =JSON.stringify( $scope.creatCons);
                var req ={
                    type:"POST",
                    uri:"updateCourseType",
                    data:{
                        jsonStr:str
                    }
                };
                httpServices.postcoursetype(req).success(function(response){
                    $scope.$apply(function(){
                       /* console.log(response.code)*///code=0代表成功
                        $scope.showpropmts=2;
                    })
                });
            }
            /*动态删除添加章节目录*/
            //获取视频列表
            function getVedioList(){
                var req ={
                    type:"GET",
                    uri:"getList",
                    data:{
                        all:true
                    }
                };
                httpServices.getSameVideo(req).success(function(response){
                    $scope.$apply(function(){
                        console.log(response)
                        $scope.vedioList = response.list;
                        console.log($scope.vedioList)
                        /*for(var i = 0;i < response.data.length;i++){
                            if(response.data[i].)
                        }*/
                    })
                });
            }
            getVedioList();
            $scope.vidCOn=[
                {id:1,name:"电子技术"},
                {id:2,name:"半导体"},
                {id:3,name:"集成电路"},
                {id:4,name:"电子技术/半导体/集成电路"}
            ]
            $scope.arrVide=[]
            $scope.arrVideId=[];
            $scope.upVedio = [{
                one:{
                    oneId:"",
                    two:{
                        twoId:"",
                        three:{
                            threeId:"",
                            threeName:""
                        }
                    }
                }
            }]
            $scope.saveVIdeo=function(item,index){
                $scope.videoId =item.videoId;
                $scope.videoCOn=item.videoName;
                $scope.bgs2=$scope.bgs=index;
            }
            $scope.addVdieoCon=function(){
                if($scope.isShowSend == "vedio"){
                    $scope.zhangNum = 0;
                    $scope.jieNum = 0;
                    if($scope.videoCOn && $scope.videoId){
                        $scope.chapterCon = [
                            {
                                chapterId:0,
                                chapterName:'第一章',
                                childChapterList:[{
                                    chapterId:0,
                                    chapterName:'第一节',
                                    videoList:[{
                                        videoId:$scope.videoId,
                                        videoName:$scope.videoCOn
                                    }]
                                }]
                            }
                        ]
                    }else{
                        alert('请选择一个视频')
                    }


                }else if($scope.isShowSend == "course"){
                    $scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList[0] = {
                        videoId:$scope.videoId,
                        videoName: $scope.videoCOn
                    };
                }

            }
            $scope.sav=function(index){
                $scope.videIndex=index;
                $scope.bgs2=index;
            }
            $scope.deleteData=function(index){
                $scope.bgs=-1;
                $scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList.splice(0,1)
                /*$scope.arrVide.splice(0,1)
                $scope.arrVideId.splice(0,1)*/
            }
            $scope.showVIdeoCon=function(index,index1){
                $scope.zhangNum = index1;
                $scope.jieNum = index;
                $scope.showV=1;
            }
            $scope.chapterCon=[];
            $scope.addCha=function(a,index){//新建章节
                var obj = {
                    chapterId: 0,
                    chapterName: a,
                    childChapterList: [
                    ]
                }
            $scope.chapterCon.push(obj);
                var height =document.querySelector("#heights").offsetHeight;
     /*           console.log(height)*/
                if(height>=712){
                    $scope.showScroll=true;
                }
            }
        $scope.delete2=function(idx){//删除章节，删除当前章节数据即可
                $scope.chapterCon.splice(idx,1);
            }
        $scope.uploadId=0;
        $scope.addCHaps=function(a,$index){
        	if($scope.chapterCon[$scope.chapterCon.length-1].childChapterList.length != 0){
        		if($scope.chapterCon[$scope.chapterCon.length-1].childChapterList[$scope.chapterCon[$scope.chapterCon.length-1].childChapterList.length-1].videoList.length==0){
	  				alert("请完善上一节信息");
	  				return
	  			}
        	}
  			
            $scope.uploadId++;
            $scope.index = $index;
                var obj = {
                    chapterId:0,
                    chapterNo:$index,
                    chapterName:a,
                    videoList: [

                    ]
                }
                $scope.chapterCon[$scope.index].childChapterList.push(obj);

            }
        $scope.$on("a",function(a,b){
            console.log(a)
            console.log(b)
        })

            $scope.delete3=function(index,index1){
                $scope.chapterCon[index1].childChapterList.splice(index,1);
            }

            /*加载数据*/
            /*课程分类数据*/
        /*一级分类*/
        function ONeClass(){
            var req ={
                type:"GET",
                uri:"getTopCategory",
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.couseONeData = response.data;
                })
            });
        }
        ONeClass();
        $scope.Choice2 = $scope.Choice3 = $scope.Choice = "请选择";
        $scope.showTwoData=function(item){
            $scope.Red2=2;
            $scope.Choice2 = $scope.Choice3 ="请选择";
            $scope.Choice = item.tcName;
            var id = item.tcId;//国内工程课程内容
            $scope.add.tcId = item.tcId;
            function oneData(){
                var req ={
                    type:"GET",
                    uri:"getSecondaryCategory?tcId=" + id,
                    data:{}
                };
                httpServices.getcourseCata(req).success(function(response){
                    $scope.$apply(function(){
                        if(response){
                            $scope.Eligible =response.data.list;
                        }else{
                            return
                        }
                    })
                });
            }
           
            oneData();
        }
        $scope.showTwoDataTwo=function(item){
            $scope.Red2=2;
            var id = item.scId;
            $scope.add.scId = item.scId;
            $scope.Choice2 = item.scName;
            $scope.Choice3 ="请选择";
            var req ={
                type:"GET",
                uri:"getThirdCategory?scId="+id,
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.ThreeList = response.data;
                })

            });
        }

        //获取讲师列表
        function getFollowList(){
            var req ={
                type:"GET",
                uri:"getFollowList",
                data:{
                    all:true,
                }
            };
            httpServices.getFollowList(req).success(function(response){
                $scope.$apply(function(){
                    $scope.follow = response.data.list;
                })
            });

        }
        getFollowList();

        $scope.ChangeCon=function(item){
            $scope.Red2=2;
            $scope.Choice3 = item.dcName;
            $scope.add.thirdId = item.dcId;
        }
        /*课程分类数据end*/
        $scope.chec2=true;
        $scope.chec3=true;
        $scope.check1=function(){
           $scope.chec=true;
           $scope.add.copyright=0;
            $scope.chec2=false;
            $scope.showRed=2;
        }
        $scope.check2=function(){
            $scope.chec=false;
            $scope.chec2=true;
            $scope.add.copyright=1;
            $scope.showRed=2;
        }
        $scope.check3=function(){
            $scope.chec3=true;
            $scope.chec4=false;
            $scope.showRed=2;
            $scope.add.visibility=1;
        }
        $scope.check4=function(){
            $scope.chec3=false;
            $scope.chec4=true;
            $scope.showRed=2;
            $scope.add.visibility=0;
        }
        $scope.check=false;
        $scope.agreeFlag = 0;
        $scope.argee=function(){
            $scope.check= !$scope.check;
            if($scope.agreeFlag == 0){
                $scope.agreeFlag = 1;
            }else if($scope.agreeFlag == 1){
                $scope.agreeFlag = 0;
            }
        }
        /*提交按钮*/

        $scope.$on('previewPath',function(a,b,c){
            console.log(b)
            $scope.add.previewPath = b;
        })
        $scope.Post=function() {
            if ( $scope.add.title == undefined ) {
                alert("请输入标题名");
                $scope.Red=1;
                $scope.nameShow=1;
            }else if($scope.Choice2 == "请选择" | $scope.Choice2 == "请选择"){
                alert("请选择课程分类")
                $scope.Red2=1;
            }else if(! $scope.chec && !$scope.chec2 ){
                alert("请选择版权")
                $scope.showRed=1;
            }else if( !$scope.chec3 && !$scope.chec4 ){
                alert("请选择费否可见")
                $scope.showRed=1;
            }else if(!$scope.check){
                alert("请同意《工程界相关服务条款》")
            }else if($scope.chapterCon.length==0){
            	alert("请输入章节内容")
            }else if($scope.chapterCon[0].chapterName==undefined){
            	alert("请完善章节信息")
            }else{
                var jsonStr = JSON.stringify($scope.chapterCon);

                var req ={
                    type:"POST",
                    uri:"add",
                    data:{
                        title:$scope.add.title,
                        labelIds:$scope.tags2.join(","),
                        tcId:$scope.add.tcId,
                        scId:$scope.add.scId,
                        thirdId:$scope.add.thirdId!=undefined?$scope.add.thirdId:"",
                        introduction:$scope.add.introduction,
                        price:$scope.add.price,
                        userId:$scope.add.userId!=""?$scope.add.userId:"",
                        copyright:$scope.add.copyright,
                        visibility:$scope.add.visibility,
                        previewPath:$scope.add.previewPath,
                        agreeFlag:$scope.agreeFlag,
                        jsonStr:jsonStr,
                        videoFlag:$scope.add.videoFlag
                    }
                };
                httpServices.getcourse(req).success(function(response){
                    $scope.$apply(function(){
                        if(response.code == 0){
                            $scope.sendSuccess = false;
                           $timeout(function(){
                               $state.go('eng.vedioDetails',{courseId:response.data,hidePlay:1});
                           },1000);
                        }else{
                            alert(response.msg);
                        }
                    })
                });
            } $scope.closeSend = function(){
                $scope.sendSuccess = true;
            }


        }
        $scope.cancelVedio = function(){
        	$scope.showV = 2;
            if($scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList.length == 0){
                $scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList.splice(0,1);
            }
        }
        $scope.saveVedio = function(){
            $scope.showV = 2;
        }

    })
});
